package ch.zbw.pra.sysventory.ui.gui.panels.statisticpanel;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ch.zbw.pra.sysventory.domain.businesslogic.DomainController;
import ch.zbw.pra.sysventory.domain.util.DateUtil;
import ch.zbw.pra.sysventory.ui.gui.utilities.DefaultFonts.FontType;
import ch.zbw.pra.sysventory.ui.gui.utilities.GuiLayout;
import ch.zbw.pra.sysventory.ui.gui.utilities.TextLayout;

public class StatisticPanel extends JPanel {

	private static final long serialVersionUID = -1251003819919214891L;
	private JLabel statisticIntroLabel;
	private JLabel osLabel;
	private JTextField osTextField;
	private JLabel swLabel;
	private JTextField swTextField;
	private JLabel countScansLastWeekLabel;
	private JTextField countScansLastWeekTextField;
	private JLabel countScansLastMonthLabel;
	private JTextField countScansLastMonthTextField;
	private JLabel countScansLastYearLabel;
	private JTextField countScansLastYearTextField;
	private DateUtil dateUtil;

	public StatisticPanel() {

		this.dateUtil = new DateUtil();
		String firstDateOfWeek = this.dateUtil.getFormattedDate(this.dateUtil.getFirstDateOfActualWeek());
		String lastDateOfWeek = this.dateUtil.getFormattedDate(this.dateUtil.getLastDateOfActualWeek());
		String firstDateOfMonth = this.dateUtil.getFormattedDate(this.dateUtil.getFirstDateOfActualMonth());
		String lastDateOfMonth = this.dateUtil.getFormattedDate(this.dateUtil.getLastDateOfActualMonth());
		String firstDateOfYear = this.dateUtil.getFormattedDate(this.dateUtil.getFirstDateOfActualYear());
		String lastDateOfYear = this.dateUtil.getFormattedDate(this.dateUtil.getLastDateOfActualYear());

		this.osLabel = new JLabel("Anzahl Betriebsysteme");
		this.osTextField = new JTextField();
		this.swLabel = new JLabel("Anzahl Software Applikationen");
		this.swTextField = new JTextField();
		this.countScansLastWeekLabel = new JLabel("Anzahl Scans in der aktuellen Woche (" + firstDateOfWeek + "-" + lastDateOfWeek + ")");
		this.countScansLastWeekTextField = new JTextField();
		this.countScansLastMonthLabel = new JLabel("Anzahl Scans im aktuellen Monat (" + firstDateOfMonth + "-" + lastDateOfMonth + ")");
		this.countScansLastMonthTextField = new JTextField();
		this.countScansLastYearLabel = new JLabel("Anzahl Scans im aktuellen Jahr (" + firstDateOfYear + "-" + lastDateOfYear + ")");
		this.countScansLastYearTextField = new JTextField();
		this.statisticIntroLabel = new JLabel("Statistische Auswertungen:");
		init();
	}

	public void init() {

		fillStatisticDatas();
		TextLayout.addStyleToComponent(this.statisticIntroLabel, FontType.BIG);

		TextLayout.addStyleToComponent(this.osLabel, FontType.MEDIUM);
		TextLayout.addStyleToTextComponentAndNotEditable(this.osTextField, FontType.MEDIUM, false);
		TextLayout.addStyleToComponent(this.swLabel, FontType.MEDIUM);
		TextLayout.addStyleToTextComponentAndNotEditable(this.swTextField, FontType.MEDIUM, false);

		TextLayout.addStyleToComponent(this.countScansLastWeekLabel, FontType.MEDIUM);
		TextLayout.addStyleToTextComponentAndNotEditable(this.countScansLastWeekTextField, FontType.MEDIUM, false);
		TextLayout.addStyleToComponent(this.countScansLastMonthLabel, FontType.MEDIUM);
		TextLayout.addStyleToTextComponentAndNotEditable(this.countScansLastMonthTextField, FontType.MEDIUM, false);
		TextLayout.addStyleToComponent(this.countScansLastYearLabel, FontType.MEDIUM);
		TextLayout.addStyleToTextComponentAndNotEditable(this.countScansLastYearTextField, FontType.MEDIUM, false);

		JPanel placeholderPanel = new JPanel();
		placeholderPanel.setBackground(Color.WHITE);
		GridBagLayout gridBagLayout = GuiLayout.getGridBagLayout(placeholderPanel);
		GridBagConstraints gridBagConstraints = GuiLayout.getGridBagConstraints();
		gridBagConstraints.insets = new Insets(10, 10, 10, 10);
		gridBagLayout.rowWeights = new double[] { 0.0, 1.0 };
		gridBagLayout.columnWidths = new int[] { 450, 150 };
		int x = 0;
		int y = 0;
		GuiLayout.addToLayout(placeholderPanel, gridBagLayout, gridBagConstraints, x, y++, this.statisticIntroLabel);
		x = 0;
		y++;
		GuiLayout.addNextRowToLayout(placeholderPanel, gridBagLayout, gridBagConstraints, x, y++, this.osLabel, this.osTextField);
		x = 0;
		y++;
		GuiLayout.addNextRowToLayout(placeholderPanel, gridBagLayout, gridBagConstraints, x, y++, this.swLabel, this.swTextField);
		x = 0;
		y++;
		GuiLayout.addNextRowToLayout(placeholderPanel, gridBagLayout, gridBagConstraints, x, y++, this.countScansLastWeekLabel, this.countScansLastWeekTextField);
		x = 0;
		y++;
		GuiLayout.addNextRowToLayout(placeholderPanel, gridBagLayout, gridBagConstraints, x, y++, this.countScansLastMonthLabel, this.countScansLastMonthTextField);
		x = 0;
		y++;
		GuiLayout.addNextRowToLayout(placeholderPanel, gridBagLayout, gridBagConstraints, x, y++, this.countScansLastYearLabel, this.countScansLastYearTextField);
		x = 0;
		y++;
		this.add(placeholderPanel);
	}

	public void fillStatisticDatas() {

		int intTempOsCount = DomainController.getInstance().getOsCount();
		int intTempSwCount = DomainController.getInstance().getSwCount();
		int intTempWeekScan = DomainController.getInstance().getAmountWeekScans();
		int intTempMonthScan = DomainController.getInstance().getAmountMonthScans();
		int intTempYearScan = DomainController.getInstance().getAmountYearScans();

		String stringTempOsCount = "0";
		String stringTempSwCount = "0";
		String stringTempWeekScan = "0";
		String stringTempMonthScan = "0";
		String stringTempYearScan = "0";

		if (intTempOsCount != 0) {
			stringTempOsCount = Integer.toString(intTempOsCount);
		}
		if (intTempSwCount != 0) {
			stringTempSwCount = Integer.toString(intTempSwCount);
		}
		if (intTempWeekScan != 0) {
			stringTempWeekScan = Integer.toString(intTempWeekScan);
		}
		if (intTempMonthScan != 0) {
			stringTempMonthScan = Integer.toString(intTempMonthScan);
		}
		if (intTempYearScan != 0) {
			stringTempYearScan = Integer.toString(intTempYearScan);
		}

		this.osTextField.setText(stringTempOsCount);
		this.swTextField.setText(stringTempSwCount);
		this.countScansLastWeekTextField.setText(stringTempWeekScan);
		this.countScansLastMonthTextField.setText(stringTempMonthScan);
		this.countScansLastYearTextField.setText(stringTempYearScan);
	}

}
